package com.zxcz.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zxcz.domain.Firmware;
import com.zxcz.domain.PageResult;
import com.zxcz.domain.Result;
import com.zxcz.domain.User;
import com.zxcz.service.FirmwareService;
import com.zxcz.tools.CustomHttpStatus;
import com.zxcz.tools.ResultUtil;

@RestController
@RequestMapping(value = "/firmware")
public class FirmwareController {
	@Autowired
	private FirmwareService firmwareService;
	private static final Log logger = LogFactory.getLog(FirmwareController.class);
	
	/**
	 * 分页
	 */
	@RequestMapping("/fingPage")
	public PageResult findPage(int pageNum, int pageSize, String userName) {
		return firmwareService.findPage(pageNum, pageSize, userName);
	}
	
	/**
	 * 新建固件包
	 * @param user
	 * @param version
	 * @param firmwareFile
	 * @param host
	 * @return
	 */
	@RequestMapping("/insert")
	public Result<Object> insertFirmware(User user, @RequestParam(defaultValue = "1") String version,
			MultipartFile firmwareFile, @RequestHeader String host) {
		
		if(!firmwareFile.isEmpty()) {
			return firmwareService.insertFirmwareIfNotPath(user, firmwareFile, version, host);
		}
		
		return ResultUtil.generate(CustomHttpStatus.FILE_IS_EMPTY);
		
	}
	
	/**
	 * 更新固件包
	 * @param firmware
	 * @param firmwareFile
	 * @param name
	 * @param host
	 * @return
	 */
	@RequestMapping("/update")
	public Result<Object> updateFirmware(Firmware firmware, MultipartFile firmwareFile, String account, @RequestHeader String host) {
		if(!firmwareFile.isEmpty()) {
			return firmwareService.updateFirmware(firmware, firmwareFile, account, host);
		}
		return ResultUtil.generate(CustomHttpStatus.FILE_IS_EMPTY);
	}
	
	
	/**
	 * 根据id删除固件包
	 */
	@RequestMapping("/delete")
	public Result<Object> deleteFirmware(int id){
		return firmwareService.deleteFirmwareById(id);
	}
}
